<template>
    <div class="white-box">
        <div class="task-one">
            <div class="task-one__item">
                <span class="task-one__label">订单编号：</span>
                <el-input placeholder="输入订单编号" v-model.trim="order_sn" clearable></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">商品名称：</span>
                <el-input placeholder="输入商品名称" v-model.trim="good_name" clearable></el-input>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">选择站点：</span>
                <el-select v-model="sites_id" clearable placeholder="请选择站点">
                    <el-option v-for="item in siteArrs" :key="item.dc_id" :label="item.title" :value="item.dc_id" />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">商品等级：</span>
                <el-select v-model="level_id" clearable placeholder="请选择商品等级">
                    <el-option :label="item.level_name" :value="item.level_id" v-for="item in pub_options.leave_list" :key="item.level_id" />
                </el-select>
            </div>
            <div class="task-one__item">
                <span class="task-one__label">订单日期：</span>
                <el-date-picker v-model="area_time" type="date" value-format="YYYY-MM-DD" :disabled-date="disabledDate" placeholder="请选择日期" />
            </div>
            <div class="task-one__btn task-wrap-btn clearfix">
                <el-button type="primary" :loading="search_btn" :icon="Search" @click="handleSearch">搜索</el-button>
            </div>
        </div>
        <!-- 批量操作 -->
        <div class="table-mb">
            <el-button type="default" plain @click="clickPrint">一键导出</el-button>
        </div>

        <el-table
            class="table-mb"
            v-loading="loading"
            :data="arr_data"
            :header-cell-style="{ background: '#F5F7FA' }"
            element-loading-background="rgba(255, 255, 255, 0.6)"
        >
            <el-table-column label="商品名称" width="300">
                <template #default="{ row }">
                    <div class="table-lr">
                        <el-image class="table-lr__img" loading="lazy" :src="proxy.imgUrl + row.product_img + '!img_400'" fit="cover" v-if="row.product_img" />
                        <div class="table-lr__right line-two">
                            {{ row.product_name }}
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="color" label="颜色"></el-table-column>
            <el-table-column label="等级">
                <template #default="{ row }">
                    <p>{{ row.level }}</p>
                </template>
            </el-table-column>
            <el-table-column label="规格">
                <template #default="{ row }">
                    <p>{{ row.norm }}/{{ row.unit }}</p>
                </template>
            </el-table-column>
            <el-table-column prop="total_num" label="数量" width="100"></el-table-column>
            <el-table-column prop="delivery_day" label="订单日期" width="160"></el-table-column>
        </el-table>

        <!-- 分页 -->
        <el-pagination
            background
            v-model:current-page="page"
            v-model:page-size="pageSize"
            :page-sizes="pageSizeNum"
            :total="totalNums"
            layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>
        <go-back />
    </div>
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, watch } from "vue"
import { Search } from "@element-plus/icons-vue"
import OneLoad from "@mixins/one-load"
import GoBack from "@components/GoBack.vue"

const { proxy } = getCurrentInstance()

onMounted(() => {
    getSite()
    pubClass()
    getList()
})

/**
 * minxin列表加载
 */
const { page, pageSizeNum, pageSize, loading, arr_data, totalNums, search_btn, pubParams, pubRefreshParams, getdata } = OneLoad()
// 切换每页显示条数
watch(pageSize, (val) => {
    page.value = 1
    pageSize.value = val
    pubParams(getList)
})
// 跳转第几页
watch(page, (val) => {
    page.value = val
    pubParams(getList)
})
function getList() {
    let params = {
        page: page.value,
        limit: pageSize.value,
        order_sn: order_sn.value,
        product_name: good_name.value,
        dc_id: sites_id.value,
        level_id: level_id.value,
        day: area_time.value,
    }
    getdata("storage/order/purchaseList", params)
}

// 搜索
const order_sn = ref("")
const area_time = ref("") // 订单日期
const good_name = ref("")
const sites_id = ref("")
const level_id = ref("")
const handleSearch = () => {
    search_btn.value = true
    pubRefreshParams(getList)
}

// 导出采购单
const clickPrint = () => {
    proxy.$axios
        .get("storage/order/allPurchaseExport", {
            params: {
                order_sn: order_sn.value,
                product_name: good_name.value,
                dc_id: sites_id.value,
                level_id: level_id.value,
                day: area_time.value,
            },
            responseType: "blob",
        })
        .then((res) => {
            let contentDisposition = res.headers["content-disposition"]
            let filename = decodeURI(contentDisposition.split("fileName=")[1] || contentDisposition.split("filename=")[1])
            let blob = new Blob([res.data], { type: res.headers["content-type"] })
            const url = window.URL.createObjectURL(blob)
            const link = document.createElement("a")
            link.href = url
            link.download = filename
            document.body.appendChild(link)
            link.click()
            URL.revokeObjectURL(url) // 释放内存
            document.body.removeChild(link)
        })
        .catch((err) => {})
}

/**
 * 获取商品分类、颜色、等级
 * @param {Array} cate_arrs 分类数组
 */
const pub_options = ref({})
function pubClass() {
    proxy.$axios
        .get("product/search")
        .then((res) => {
            if (res.data.code == 0) {
                let result = res.data.data
                pub_options.value = result
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {})
}

// 站点信息
const siteArrs = ref([])
function getSite() {
    proxy.$axios
        .get("operation/dc")
        .then((res) => {
            if (res.data.code == 0) {
                let result = res.data.data
                siteArrs.value = result
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {})
}

const disabledDate = (time) => {
    return time.getTime() > Date.now()
}
</script>
<script>
export default {
    name: "PurchaseOrder",
}
</script>

<style lang="less" scoped>
.table-lr__img {
    width: 40px;
    height: 40px;
}
.table-lr__right {
    min-height: 40px;
}
</style>
